package cn.com.haopy.yue.ai.admin.web;

import cn.com.haopy.yue.ai.admin.annotation.RequiresPermissionsDesc;
import cn.com.haopy.yue.ai.admin.dto.CalculateInfoDto;
import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskStandardCalculate;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskStandardCalculateItem;
import cn.com.haopy.yue.ai.db.entity.arisk.HOSAdmin;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskStandardCalculateItemService;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskStandardCalculateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Description: TODO
 * @Author DuTao
 * @Date 2025/3/26 9:21
 */
@RestController
@RequestMapping("/admin/standardCalculate")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "计算公式")
public class AdminAiRiskStandardCalculateController {

    @Autowired
    private AIRiskStandardCalculateService standardCalculateService;

    @Autowired
    private AIRiskStandardCalculateItemService standardCalculateItemService;


    @RequiresPermissions("admin:standardCategory:save")
    @RequiresPermissionsDesc(menu = {"运营管理", "计算公式"}, button = "添加")
    @PostMapping("/save")
    @ApiOperation("新增")
    @Transactional
    public Object create(@RequestBody @Validated AIRiskStandardCalculate category) {

        Subject currentUser = SecurityUtils.getSubject();
        HOSAdmin admin = (HOSAdmin) currentUser.getPrincipal();
        category.setCreateBy(admin.getUsername());
        category.setUpdateBy(admin.getUsername());
        boolean isSuccess;
        if (Objects.nonNull(category.getId())) {
            isSuccess = standardCalculateService.updateById(category);
        } else {
            isSuccess = standardCalculateService.save(category);
        }

        if (!isSuccess) {
            return ResponseUtil.fail("添加失败");
        }
        return ResponseUtil.ok();
    }


    @GetMapping("/calculateDetail")
    @ApiOperation("计算公式详情")
    public Object calculateDetail(@RequestParam @ApiParam("计费标准ID") Integer chargeId,
                                  @RequestParam(required = false) @ApiParam("分类cateId") Integer cateId
    ) {

        AIRiskStandardCalculate calculate = standardCalculateService.getByChargeId(chargeId, cateId);
        if (Objects.nonNull(calculate)) {
            CalculateInfoDto infoDto = new CalculateInfoDto();
            BeanUtils.copyProperties(calculate, infoDto);

            List<AIRiskStandardCalculateItem> items = standardCalculateItemService.getByCuId(calculate.getId());

            infoDto.setItems(items);

            return ResponseUtil.ok(infoDto);
        }
        return ResponseUtil.fail();
    }
}
